import Vue from 'vue'

export const USER_SIGNIN = 'USER_SIGNIN' //登录成功
export const USER_SIGNOUT = 'USER_SIGNOUT' //退出成功
export function isLogin(){
    return Boolean(sessionStorage.getItem('user'))
}

const user = {
    state: JSON.parse(sessionStorage.getItem('user')) || {},
    mutations: {
        [USER_SIGNIN](state, user) {
            debugger
            if(user){
                sessionStorage.setItem('user', JSON.stringify(user));
                Object.assign(state, user)
            }
        },
        [USER_SIGNOUT](state) {
            sessionStorage.removeItem('user')
            Object.keys(state).forEach(k => Vue.delete(state, k))
        }
    },
    actions: {
        [USER_SIGNIN]({commit},user){
            debugger
            commit(USER_SIGNIN,user);
        },
        [USER_SIGNOUT]({commit}){
            commit(USER_SIGNOUT)
        }
    }
}

export default user